<?php

class OrderController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	}
	
	public function indexAction() {
		try { 
		 //查询待确认的
		 $sql="select count(*) as num from student_order_goods_v  where  ischecked=0 and isdelete=0";
		 $check = $this->_dbAdapter->fetchOne ($sql); 
		 $this->view->check= $check;
		 //查询所有待发货的
		 $sql="select count(*) as num from  student_order_goods_v  where  ischecked=1 and isship=0 and isdelete=0";
		 $checked = $this->_dbAdapter->fetchOne ($sql);
		 $this->view->checked= $checked;  
		 //查询所有已发货的
		 $sql="select count(*) as num from  student_order_goods_v  where  ischecked=1 and isship=0 and isdelete=0";
		 $ship = $this->_dbAdapter->fetchOne ($sql);
		 $this->view->ship= $ship;   
		 //查询所有的历史订单
		 $sql="select count(*) as num from  student_order_goods_v  where  ischecked=1 and isship=1 and isover=1 and isdelete=0";
		 $over = $this->_dbAdapter->fetchOne ($sql);  
		 $this->view->over= $over;  
		 //查询所有的作废订单
		 $sql="select count(*) as num from  student_order_goods_v  where  isdelete=1";
		 $cancel = $this->_dbAdapter->fetchOne ($sql);   
		 $this->view->cancel= $cancel;  
	   
	     } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//待确认
	public function checkAction() {
		try {
		 //查询所有的未确认订单
		    $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			  //$sql="select SQL_CALC_FOUND_ROWS *  from  credit_orders_logs_goods_v  where  ischecked=0  and  isdelete=0   limit $offset,$count"; 
				
				$sql="select SQL_CALC_FOUND_ROWS *  from  student_order_goods_v  where  ischecked=0  and  isdelete=0   limit $offset,$count";  
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			  // print_r($result);
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
			 //查询学生对应的机构信息
			 
			 $sql="select   uid_student,groupname,schoolname,classname,groupid,schoolid,classid  from  group_school_class_student_v   ";  
			 $grouplist = $this->_dbAdapter->fetchAssoc ($sql);	 
			 $this->view->grouplist= $grouplist;	 
			 // print_r($grouplist);
			 
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function confirmorderAction() {
		try {
	        $orderid=$this->_request->getParam('id');    
		    $page=$this->_request->getParam('pageNum'); 
	        if(empty($orderid))
				throw new Exception ( 'parameter_error' );
			//$data=array();
			//$data['ischecked']='1'	;
	        $result = $this->_dbAdapter->update ('stu_orders',array('ischecked'=>'1','ischeckedtime'=>time()),'orderid='.$orderid);   
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/check/page/'.$page, 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}
	 
	 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//未发货 
	public function deliveryAction() {
	  try {
	        $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from  student_order_goods_v  where  ischecked=1  and  isship=0 and isdelete=0   limit $offset,$count";  
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
			  $sql="select   uid_student,groupname,schoolname,classname,groupid,schoolid,classid  from  group_school_class_student_v   ";  
			 $grouplist = $this->_dbAdapter->fetchAssoc ($sql);	 
			 $this->view->grouplist= $grouplist;
			 
	
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//确认发货
	
	public function   sendgoodsAction() {
	  try {
	        $orderid=$this->_request->getParam('id');    
		    $page=$this->_request->getParam('pageNum'); 
			$uid=$this->_request->getParam('uid'); 
			$goodsname=$this->_request->getParam('goodsname');
			$groupid=$this->_request->getParam('groupid');
			$schoolid=$this->_request->getParam('schoolid');
			 
			
	        if(empty($orderid)){
				throw new Exception ( 'parameter_error' );
			}
			if(empty($uid)){
				throw new Exception ( 'parameter_error' );
			}
			if(empty($groupid)){
				throw new Exception ( 'parameter_error' );
			}
			if(empty($schoolid)){
				throw new Exception ( 'parameter_error' );
			}
			//$data=array();
			//$data['ischecked']='1'	;
	        $result = $this->_dbAdapter->update ('stu_orders',array('isship'=>'1','isshiptime'=>time()),'orderid='.$orderid);           $isok=0;
			  if($result){
			    $letter=array();
				$letter['letter_type']='txt';
				$letter['from_role']='admin';
				$letter['from_uid']='0';
				$letter['to_role']='teacher';
				$letter['to_uid']=$uid;
				$letter['content']='您购买的商品'.$goodsname.'已经为您发货，请注意查收！';
				$letter['addtime']=time();  
			    $letternum = $this->_dbAdapter->insert ('letter',$letter);	 
			  
				if($letternum){ 
					$isok=1; 
					} else{
					  $isok=0;	
				      } 
				  }else{
				   $isok=0;		 
				  }  
			 
		 if ($isok>0) {
				$this->feedback ( '确认发货', '成功', '/'.$this->controller.'/delivery/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}
	   
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 
	//已经发货的
	public function  shippedAction() {
	  try {
	        $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from  student_order_goods_v  where  ischecked=1  and  isship=1 and isover=0 and isdelete=0   limit $offset,$count";  
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
			 
			  $sql="select   uid_student,groupname,schoolname,classname,groupid,schoolid,classid  from  group_school_class_student_v   ";  
			 $grouplist = $this->_dbAdapter->fetchAssoc ($sql);	 
			 $this->view->grouplist= $grouplist;
			 
	
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//确认收货 
	public function   shipedgoodsAction() {
	  try {
	        $orderid=$this->_request->getParam('id');    
		    $page=$this->_request->getParam('pageNum'); 
	        if(empty($orderid))
				throw new Exception ( 'parameter_error' );
			//$data=array();
			//$data['ischecked']='1'	;
	        $result = $this->_dbAdapter->update ('stu_orders',array('isover'=>'1','isovertime'=>time()),'orderid='.$orderid);   
			if ($result) {
				$this->feedback ( '已成功收货', '成功', '/'.$this->controller.'/shipped/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}
	
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//历史订单
	 
	public function  historyAction() {
	  try {
	        $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from  student_order_goods_v  where  isover=1   limit $offset,$count";  
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
			  $sql="select   uid_student,groupname,schoolname,classname,groupid,schoolid,classid  from  group_school_class_student_v   ";  
			 $grouplist = $this->_dbAdapter->fetchAssoc ($sql);	 
			 $this->view->grouplist= $grouplist;
			 
			 
	
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//作废订单
	public function  cancelorderAction() {
	  try {
	
	       $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from  student_order_goods_v  where  isdelete=1   limit $offset,$count";  
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
	          
			  $sql="select   uid_student,groupname,schoolname,classname,groupid,schoolid,classid  from  group_school_class_student_v   ";  
			 $grouplist = $this->_dbAdapter->fetchAssoc ($sql);	 
			 $this->view->grouplist= $grouplist;	
			        
	
	     } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
public function reductionAction() {
		try {	
	        $orderid=$this->_request->getParam('id');    
		    $page=$this->_request->getParam('pageNum'); 
	        if(empty($orderid))
				throw new Exception ( 'parameter_error' );
			  
	         $result = $this->_dbAdapter->update ('stu_orders',array('isdelete'=>0,'isdeletetime'=>0),'orderid='.$orderid);   
			//扣除积分
		/*	$isok=0;
			if($result){
				$sql="select * from credit_orders_logs_goods_v where orderid='".$orderid."'";
				$ordersOne = $this->_dbAdapter->fetchRow($sql); 
			   //  print_r($ordersOne);  exit;
			   $sql = 'update class set credit=credit-"'.$ordersOne['credit'].'"  where uid='.$ordersOne['class_uid'];   
			   $addcredit = $this->_dbAdapter->query ($sql);
					 if($addcredit){
							$isok=1;
						 }else{
							$isok=0;	 
							 }
			   }else{
				 $isok=0;
				}*/
			
			
			if ($result ) { 
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			} 
	 
	
	
	    } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
   }
	public function deleteAction() {
		try {
	        $orderid=$this->_request->getParam('id'); 
			//$logid=$this->_request->getParam('logid');    
		    $page=$this->_request->getParam('pageNum'); 
	        if(empty($orderid))
				throw new Exception ( 'parameter_error' ); 
	         $result = $this->_dbAdapter->update ('stu_orders',array('isdelete'=>1,'isdeletetime'=>time()),'orderid='.$orderid);   
			//退还积分
			$isok=0;
			
			if($result){
				/*
				//$sql="select * from credit_orders_logs_goods_v where orderid='".$orderid."'";
				//$ordersOne = $this->_dbAdapter->fetchRow($sql); 
			   //  print_r($ordersOne);  exit;
			  // $sql = 'update class set credit=credit+"'.$ordersOne['credit'].'"  where uid='.$ordersOne['class_uid'];   
			  // $addcredit = $this->_dbAdapter->query ($sql);
				
				$sql = 'update credit_logs set isdelete=1  where creditlogid='.$logid;   
			    $addcredit = $this->_dbAdapter->query ($sql); 
				 if($addcredit){
					    $isok=1;
					 }else{
					    $isok=0;	 
			 		 }*/
					 $isok=1;	
			}else{
				$isok=0;
				} 
			
			if ($isok > 0 ) { 
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			} 
	 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

